﻿/******************************************************************
    + FACEBOOK AUTO LOGIN
    + PHUHAIGIA-GROUP
    + AUTHOR: NGUYEN7HUAN
*******************************************************************/
// Init the SDK upon load
window.fbAsyncInit = function() {
    var fbStatus = false;
    var fbId = "";
    var fbName = "";
    var fbEmail = "";
    var fbGender = "";
    var fbBirthday = "";

    FB.init({
        appId: '468794623180533', // App ID
        channelUrl: '//' + window.location.hostname + '/channel', // Path to your Channel File
        status: true, // check login status
        cookie: true, // enable cookies to allow the server to access the session
        xfbml: true  // parse XFBML
    });

    // listen for and handle auth.statusChange events
    // Tự động kiểm tra trạng thái đăng nhập facebook
    FB.Event.subscribe('auth.statusChange', function (response) {
        if (response.authResponse) {
            // user has auth'd your app and is logged into Facebook
            FB.api('/me', function (me) {
                if (me.id) {
                    fbId = me.id;
                }
                if (me.name) {
                    fbName = me.name;
                }
                if (me.email) {
                    fbEmail = me.email;
                }
                if (me.gender) {
                    fbGender = me.gender;
                }
                if (me.birthday) {
                    fbBirthday = me.birthday;
                }
                clearInfo(fbId, fbName, fbEmail, fbGender, fbBirthday)
                saveInfo(fbId,fbName,fbEmail,fbGender,fbBirthday)
            })
        } else {
            // user has not auth'd your app, or is not logged into Facebook
            clearInfo(fbId, fbName, fbEmail, fbGender, fbBirthday)
        }
    });
    //$("#auth-logoutlink").click(function () { FB.logout(function () { window.location.reload(); }); });
}

//Save info user
function saveInfo(fbId, fbName, fbEmail, fbGender, fbBirthday) {
    createCookie('fbId', fbId, 7);
    createCookie('fbName', fbName, 7);
    createCookie('fbEmail', fbEmail, 7);
    createCookie('fbGender', fbGender, 7);
    createCookie('fbBirthday', fbBirthday, 7);
}

//Clear Cookie
function clearInfo(fbId, fbName, fbEmail, fbGender, fbBirthday) {
    eraseCookie('fbId');
    eraseCookie('fbName');
    eraseCookie('fbEmail');
    eraseCookie('fbGender');
    eraseCookie('fbBirthday');
}